<?php
namespace app\backend\model;

use \think\Db;
use traits\model\SoftDelete;


class Tab extends \think\Model
{
    //软删除
    use SoftDelete;
    static protected $deleteTime = 'delete_time';

    public function getnav(){
        // 查询数据
        $data =$this
            ->field('tab_id,tab_name_cn,tab_name_en,p_id')
            ->where(array('status'=>1,'tab_id'=>array('exp','not in(1,2,3)')))
            ->select()
            // ->where("p_id = $pid")
            /*->paginate(5)*/;
//self对类自己本身的引用
        $arr=self::_get_child($data); //得到菜单数组
        return $arr; //返回json格式
    }


    //分类级别
    static protected function _cate_level($data, $pid=0, $level=0){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['p_id'] == $pid){
                $v['level'] = $level;
                $array[] = $v;
                unset($data[$k]);
                $array = array_merge($array, self::_cate_level($data, $v['tab_id'],$level+1)); //递归
            }
        }
        return $array;
    }

    //模块管理index展示
    public function getAll($id)
    {
        $data=$this
            ->field('tab_id,p_id,tab_name_cn,tab_name_en,update_time,tab_url')
            //->where(["p_id"=>array('exp','<>0')])
            ->where(["p_id"=>"$id"])//$id不等于0
            /*->order("is_recommend DESC")*/;
        //->select();
        return $data;
    }


    //菜单
    static protected function _get_child($data, $pid=0){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['p_id'] == $pid){
                $v['child'] = self::_get_child($data, $v['tab_id']); //递归
                $array[] = $v;
            }
        }
        return $array;
    }




}